package com.huitone.gddw.service.impl.monitoring.sms;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.monitoring.sms.ISvrNodeReportDao;
import com.huitone.gddw.service.dto.monitoring.sms.SvrNodeReportACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.SvrNodeReportDTO;
import com.huitone.gddw.service.dto.monitoring.sms.SvrNodeUpRepACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.SvrNodeUpRepDTO;
import com.huitone.gddw.service.monitoring.sms.ISvrNodeReportService;
import com.huitone.gddw.utils.DateTimeUtils;

/**
 * 业务端监控 接口实现类
 * @author wuluodan
 *
 */
@Service
public class SvrNodeReportService implements ISvrNodeReportService {

	@Autowired
	private ISvrNodeReportDao svrNodeReportDao;
	
	@Override
	public SvrNodeReportACDTO find(Map<String, Object> params) {
		boolean isNow = this.hasToday(params);
		List<SvrNodeReportDTO> all = new ArrayList<SvrNodeReportDTO>();
		List<SvrNodeReportDTO> list = svrNodeReportDao.find(params);
//		SvrNodeReportACDTO snr = svrNodeReportDao.findCount(params);
		SvrNodeReportACDTO snr = svrNodeReportDao.findCount(params);
		if (isNow) {
//			String today = DateTimeUtils.dateToStr(new Date(), "yyyy-MM-dd");
//			Map<String, Object> curDate = new HashMap<String, Object>();
//			curDate.put("startDate", today);
//			curDate.put("endDate", today);
			List<SvrNodeReportDTO> snrList = svrNodeReportDao.selectSendSm(params);
			if (snrList != null && snrList.size() > 0) {
//				SvrNodeReportDTO s = svrNodeReportDao.selectRecvSm(curDate);
//				svrNodeReportDTO.setUpTotal(s == null ? 0 : s.getUpTotal());
				all.addAll(snrList);
				
				SvrNodeReportACDTO svrNodeReportACDTO = svrNodeReportDao.countSendSm(params);
				if (svrNodeReportACDTO != null) {
					snr.setTotalCount(snr.getTotalCount() + svrNodeReportACDTO.getTotalCount());
					snr.setSendSucCount(snr.getSendSucCount() + svrNodeReportACDTO.getSendSucCount());
					snr.setSendFailCount(snr.getSendFailCount() + svrNodeReportACDTO.getSendFailCount());
					snr.setSendSucRateCount(snr.getTotalCount() == 0 
							? "0%" : String.format("%.2f", (float)snr.getSendSucCount() / snr.getTotalCount() * 100) + "%");
					snr.setRecvSucCount(snr.getRecvSucCount() + svrNodeReportACDTO.getRecvSucCount());
					snr.setRecvFailCount(snr.getRecvFailCount() + svrNodeReportACDTO.getRecvFailCount());
					snr.setRecvNoneCount(snr.getRecvNoneCount() + svrNodeReportACDTO.getRecvNoneCount());
				}
			}
		}
		all.addAll(list);
		snr.setList(all);
		return snr;
	}

	@Override
	public SvrNodeUpRepACDTO findUp(Map<String, Object> params) {
		List<SvrNodeUpRepDTO> all = new ArrayList<SvrNodeUpRepDTO>();
		List<SvrNodeUpRepDTO> list = svrNodeReportDao.findUp(params);
		SvrNodeUpRepACDTO snr = svrNodeReportDao.findCountUp(params);
		boolean isNow = this.hasToday(params);
		if (isNow) {
//			String today = DateTimeUtils.dateToStr(new Date(), "yyyy-MM-dd");
//			Map<String, Object> curDate = new HashMap<String, Object>();
//			curDate.put("startDate", today);
//			curDate.put("endDate", today);
			List<SvrNodeUpRepDTO> snrList = svrNodeReportDao.selectRecvSm(params);
			if (snrList != null && snrList.size() > 0) {
				all.addAll(snrList);
				SvrNodeUpRepACDTO svrNodeUpRepACDTO = svrNodeReportDao.countRecvSm(params);
				if (svrNodeUpRepACDTO != null) {
					snr.setUpTotalCount(snr.getUpTotalCount() + svrNodeUpRepACDTO.getUpTotalCount());
				}
			}
		}
		all.addAll(list);
		snr.setList(all);
		return snr;
	}
	
	/**
	 * 判断是否查询当天
	 * @param params
	 * @return
	 */
	private boolean hasToday(Map<String, Object> params) {
		boolean isNow = true;
		Date now = new Date();
		if (params.get("startDate") != null && !"".equals(params.get("startDate").toString())) {
			Date startDate = DateTimeUtils.strToDate(params.get("startDate").toString() + " 00:00:00");
			if (startDate.after(now)) {
				isNow = false;
			}
		}
		if (isNow && params.get("endDate") != null && !"".equals(params.get("endDate").toString())) {
			Date endDate = DateTimeUtils.strToDate(params.get("endDate").toString() + " 23:59:59");
			if (endDate.before(now)) {
				isNow = false;
			}
		}
		return isNow;
	}
	
}
